<%@ Page Title="" Language="C#" MasterPageFile="~/Themes/Default/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcContrib.UI.Html" %>


<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	<%= Html.Resource("Department","TITLE_DEPARTMENTMANAGEMENT")%>
</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="PostScript" runat="server">
    <script type="text/javascript" language="javascript">
        var isCreate = false;

   //render function for /user/popup data
    function renderData(data, textStatus)
    {
        var rows = eval(data);
        for (var i = 0; i < rows.length; i++) {
            var rowdata = rows[i];
            //rowdata.cell.act="<div align='center'><%=Html.RenderjQueryIcon("deletebtn","ui-icon-circle-close","Remove from this role",null,"rowid='\"+rowdata.id+\"'") %></div>";
            jQuery("#list").addRowData(rowdata.id, rowdata.cell);
            //jQuery("#"+rowdata.id+" .deletebtn").click(deleteButtonClick);
        }
    }

        $(document).ready(function() {
            jQuery("#list").jqGrid({
                url: '/Department/UsersInDepartmentDetail?q=1&departid=0',
                datatype: 'json',
                mtype: 'GET',
                colNames: ['ID', '<% =Html.Resource("User","username") %>', '<% =Html.Resource("User","Email") %>', '<% =Html.Resource("User","CREATIONDATE") %>', '<% =Html.Resource("User","LASTLOGINDATE") %>'],
                colModel: [
          { name: 'id', index: 'id', hidden: true },
          { name: 'username', index: 'username', width: 100, align: 'left' },
          { name: 'email', index: 'email', width: 100, align: 'left' },
          { name: 'createdate', index: 'createdate', width: 100, align: 'left' },
          { name: 'lastlogindate', index: 'lastlogindate', width: 100, align: 'left'}],
                pager: jQuery('#pager'),
                rowNum: 20,
                rowList: [20, 50],
                sortname: 'username',
                sortorder: "asc",
                viewrecords: true,
                imgpath: '/scripts/themes/coffee/images',
                caption: '',
                hidegrid: true,
                //width: 700,
                autowidth: true,
                height: 300,
                multiselect: true,
            }).navGrid('#pager', {
                refresh: true,
                edit: false,
                add: false,
                del: false,
                search: false
            }).navButtonAdd('#pager', {
                caption: '<%=Html.Resource("Global","REMOVE") %>',
                title: '<%=Html.Resource("Department","DELETEUSERFROMDEPARTMENT") %>',
                buttonicon: "ui-icon-trash",
                onClickButton: function() {
                    var node=jQuery.tree.focused().selected;
                    if(node)
                    {
                        var departmentid=$(node).attr("departmentid");
                        if(departmentid=="8")
                        {
                            alert("Unassigned Users cannot be removed");
                            return;
                        }
                    }
                    
                    var array_contents=jQuery("#list").getGridParam('selarrrow');
                    if (array_contents.length == 0) {
                        return;
                    }
                    var userids=new Array();
                    var strQuery=new Array();
                    for(var i=0;i<array_contents.length;i++)
                    {
                        userids.push(array_contents[i]);
                        strQuery.push("userids="+array_contents[i]);
                    }
                    jQuery("#list").delGridRow(userids.join(','),{
                        modal:false,
                        caption:'<% =Html.Resource("Global","CONFIRMDIALOG") %>',
                        msg:'<% =Html.Resource("Role","DELETEUSERMESSAGE") %>',
                        bSubmit:'<% =Html.Resource("Global","YES") %>',
                        bCancel:'<% =Html.Resource("Global","NO") %>',
                        reloadAfterSubmit:false,
                        url:'/Department/SetUnassignedUsers/?'+strQuery.join('&')
                    });
                },
                position: "first"
            }).navButtonAdd('#pager', {
                caption: '<%=Html.Resource("Global","ADD") %>',
                title: '<%=Html.Resource("Department","ADDUSERTODEPARTMENT") %>',
                buttonicon: "ui-icon-plus",
                onClickButton: function() {
                    var node=jQuery.tree.focused().selected;
                    if(!node)
                    {
                        alert('<%=Html.Resource("Department","MSG_SELECT_DEPARTMENT_FIRST") %>');
                        return;
                    }
                    var departmentid=$(node).attr("departmentid");
                    open_popup("/User/Popup/",550,500,true,true,{
                    "call_back_function":"set_return_and_save_background",
                    "form_name":"form1",
                    "field_to_name_array":{"id":"userids"},
                    "passthru_data":{
	                    "child_field":"users",
	                    "link_field_name":"users",
	                    "module_name":"user",
	                    "return_url":"/Department/AssignUsersToDepartment?departid="+departmentid,
	                    "refresh_page":"0"}},
	                "multiselect");
                },
                position: "first"
            });
            jQuery("#treeview").tree({
                types: {
                    "default": { draggable: false }
                },
                plugins : {
                    contextmenu : { },
                    hotkeys:{}
                },
                ui: { theme_name: "classic" },
                callback: {
                    beforecreate: function() {
                        //console.log("before create");
                        return true;
                    },
                    ondblclk:function(node,treeObj){
                       
                        jQuery("#list").setGridParam({url:"/Department/UsersInDepartmentDetail?q=1&departid="+$(node).attr("departmentid"),page:1}).trigger('reloadGrid');
                        return true;
                    },
                    oncreate: function(node, refnode, type, treeObj, rb) {
                        isCreate = true;
                        //console.log(treeObj.get_text(node));
                        //console.log("create a new node");
                    },
                    beforedelete: function(node, treeObj) {
                        if (confirm("Are you sure to delete the department: " + treeObj.get_text(node))) {
                            return true;
                        }
                        return false;
                    },
                    ondelete: function(node, treeObj) {
                        jQuery.post("/Department/DeleteDepartment",
                                { id: jQuery(node).attr("departmentID") },
                                function(data) {
                                    //console.log(data);
                                    //console.log("delete a node");
                                });
                    },
                    onrename: function(node, treeObj) {
                        if (isCreate) {
                            jQuery.post("/Department/CreateDepartment",
                                { departname: treeObj.get_text(node), parentid: treeObj.parent(node).attr("departmentID") },
                                function(data) {
                                    jQuery(node).attr("departmentID", data);
                                })
                            return;
                        }
                        jQuery.post("/Department/RenameDepartment",
                                { id: jQuery(node).attr("departmentID"), departname: treeObj.get_text(node) },
                                function(data) {
                                    //console.log(data);
                                    //console.log("rename a node");
                                });
                        isCreate = false;
                    }
                }
            });
            //expand all tree nodes
            jQuery.tree.focused().open_all(); 
            //customize the buttons
            
            jQuery(".btnTransferStaff").click(function() {
                redirectToAction("Department","TransferStaffBetweenDepartment");
            });
            
//            jQuery(".btnCreate").click(function() {
//                var t = jQuery.tree.focused();
//                if (t.selected) {
//                    t.create();
//                }
//                else
//                    alert("Select a node");
//            });
//            jQuery(".btnDelete").click(function() {
//                var t = jQuery.tree.focused();
//                if (t.selected) {
//                    t.remove();
//                }
//                else
//                    alert("Select a node");
//            });
//            jQuery(".btnRename").click(function() {
//                var t = jQuery.tree.focused();
//                if (t.selected)
//                    t.rename();
//                else
//                    alert("Select a node");
//            })

        });
    </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <form id="form1" method="post">
    <h2><%= Html.Resource("Department","TITLE_DEPARTMENTMANAGEMENT")%></h2>
    <table>
    <tr>
    <td colspan="2">
    <input type="button" value="<% =Html.Resource("Department","Transfer_Staff") %>" class="btnTransferStaff" />
    </td>
    </tr>
    <tr><td style="width:220px">
    <div class="tree-themeroller ui-widget-content" id="treeview" style="width:218px; height:350px; ">
    <ul>
        <%Html.RenderPartial("DepartmentItemControl", ViewData.Model,ViewData);%>
    </ul>
    </div>
    </td>
    <td>
        <table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
        <div id="pager" class="scroll" style="text-align:center;"></div>
    </td>
    </tr>
    <tr>
    </tr>
    </table>
    </form>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
        <%=Html.Stylesheet("~/Content/jstree/themes/themeroller/style.css")%>

    <%=Html.ScriptInclude("~/Scripts/sarissa.js") %>
    <%=Html.ScriptInclude("~/Content/jstree/jquery.tree.min.js") %>
    <%=Html.ScriptInclude("~/Content/jstree/plugins/jquery.tree.contextmenu.js")%>
    <%=Html.ScriptInclude("~/Content/jstree/plugins/jquery.tree.hotkeys.js")%>
    <%=Html.ScriptInclude("~/Content/jstree/plugins/jquery.tree.themeroller.js")%>
        <%=Html.Stylesheet("~/Content/jgGrid/ui.jqgrid.css")%>
	    <%=Html.ScriptInclude("~/Scripts/jgGrid/i18n/grid.locale-"+Html.GetjqGridCulture()+".js")%>
        <%=Html.ScriptInclude("~/Scripts/jgGrid/jquery.jqGrid.min.js")%>
        <%=Html.ScriptInclude("~/Scripts/popupHelper.js")%>
</asp:Content>
